package com.liangwei.productAndConsumer;

import java.util.Date;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class jugConditionPC {
    public static void main(String[] args) {
        Data data = new Data();
        //创建消耗的线程
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.printA();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.printB();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.printC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();
    }
    /** * @author liangwei
     * A 执行完调用B，B执行完调用C，C执行完调用A */
    static class Data {
        //创建资源类
        private int number = 1;
        //创建condition监听器对象
        Lock lock = new ReentrantLock();
        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();
        Condition condition3 = lock.newCondition();
        //创建消耗资源的方法
        public void printA() throws InterruptedException {
            //上锁
            lock.lock();
            try {
                //编写消耗的业务
                while (number != 1) {
                    condition1.await();
                }
                System.out.println(Thread.currentThread().getName() + "=>AAAAAAAAAAAA");
                //唤醒指定的人，B
                number = 2;
                //通知其他的线程
                condition2.signalAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                //释放锁
                lock.unlock();
            }
        }
        public void printB() throws InterruptedException{
            //上锁
            lock.lock();
            try {
                //编写消耗的业务
                while (number != 2) {
                    condition2.await();
                }
                System.out.println(Thread.currentThread().getName() + "=>BBBBBBBBBBB");
                number = 3;
                condition3.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                //释放锁
                lock.unlock();
            }
        }
        public void printC() throws InterruptedException{
            //上锁
            lock.lock();
            try {
                //编写消耗的业务
                while (number != 3) {
                    condition3.await();
                }
                System.out.println(Thread.currentThread().getName() + "=>CCCCCCCCCCCCC");
                number = 1;
                //通知其他的线程
                condition1.signalAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                //释放锁
                lock.unlock();
            }
        }
    }
}
